﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using SW.Lite.BLL.Master;
using SW.Lite.Core.Attributes;
using SW.Lite.Core.Authority;
using SW.Lite.Infrastructure.Common;
using SW.Lite.Infrastructure.Helpers;
using SW.Lite.Model.Master;

namespace SW.Lite.WebSite.Areas.Master.Controllers
{
    /// <summary>
    /// Class MenuController.
    /// </summary>
    public class MMenuController : BaseController
    {
        /// <summary>
        /// Indexes the specified page.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="parentId">The parent identifier.</param>
        /// <returns>ActionResult.</returns>
        [Menu(MenuCode = "MMenuManage")]
        public ActionResult Index(int page, int? parentId)
        {
            var id = parentId;
            var menus = Menu.All(parentId ?? 0, page, PageSize);

            ViewBag.RecordCount = menus.AllCount;
            ViewBag.ParentID = parentId;

            var menuQueue = new Stack<MenuModel>();
            if (parentId != null)
            {
                while (parentId.Value != 0)
                {
                    var menu = Menu.Single(parentId.Value);
                    menuQueue.Push(menu);
                    parentId = menu.ParentID;
                }
            }
            ViewBag.MenuQueue = menuQueue;

            var iconFolder = AppDomain.CurrentDomain.BaseDirectory + "Areas\\Master\\Common\\icons\\24";
            var files = Directory.GetFiles(iconFolder);
            var dic = new Dictionary<string, string>();
            foreach (var file in files)
            {
                var fileNameWithoutEx = Path.GetFileNameWithoutExtension(file);
                var fileName = Path.GetFileName(file);
                if (fileNameWithoutEx != null)
                    dic.Add(fileNameWithoutEx, fileName);
            }
            ViewBag.Icons = dic;
            return View(menus);
        }

        /// <summary>
        /// Adds the specified menu.
        /// </summary>
        /// <param name="menu">The menu.</param>
        /// <returns>JsonResult.</returns>
        [Button(MenuCode = "MMenuManage", ButtonCode = "Add", IsAjax = true)]
        public JsonResult Add(MenuModel menu)
        {
            var result = Menu.Insert(menu);

            return result ? new JsonResult { Data = new ExecuteResult { Result = true } } : new JsonResult { Data = new ExecuteResult { Result = false, Message = result.Message } };
        }

        /// <summary>
        /// Edits the specified menu.
        /// </summary>
        /// <param name="menu">The menu.</param>
        /// <returns>JsonResult.</returns>
        [Button(MenuCode = "MMenuManage", ButtonCode = "Add", IsAjax = true)]
        public JsonResult Edit(MenuModel menu)
        {
            var result = Menu.Update(menu);

            return result ? new JsonResult { Data = new ExecuteResult { Result = true } } : new JsonResult { Data = new ExecuteResult { Result = false, Message = result.Message } };
        }

        /// <summary>
        /// Deletes the specified ids.
        /// </summary>
        /// <param name="ids">The ids.</param>
        /// <returns>JsonResult.</returns>
        [Button(MenuCode = "MMenuManage", ButtonCode = "DeleteAll", IsAjax = true)]
        public JsonResult Delete(string ids)
        {
            if (ids.Length <= 0)
                return new JsonResult { Data = new ExecuteResult { Message = "请选择删除的菜单" } };

            var et = ids.Split(',').Select(n => Convert.ToInt32(n));

            var result = Menu.Delete(et.ToArray());

            return result ? new JsonResult { Data = new ExecuteResult { Result = true } } : new JsonResult { Data = new ExecuteResult { Result = false, Message = result.Message } };
        }

        /// <summary>
        /// Singles the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns>JsonResult.</returns>
        [HttpPost]
        [Button(MenuCode = "MMenuManage", ButtonCode = "Add,Edit", IsAjax = true)]
        public JsonResult Single(int id)
        {
            var menu = Menu.Single(id);
            return new JsonResult
            {
                Data = new
                    {
                        menu.Id,
                        menu.ParentID,
                        menu.ActionName,
                        menu.Code,
                        menu.ControllerName,
                        menu.Icon,
                        menu.Sort,
                        menu.Title
                    }
            };
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <returns>JsonResult.</returns>
        [HttpPost]
        [Button(MenuCode = "MMenuManage", ButtonCode = "Save", IsAjax = true)]
        public JsonResult Save()
        {
            var sorts = Newtonsoft.Json.JsonConvert.DeserializeObject<List<KeyValuePair<int, int>>>(RequestHelper.Instance.FormString("sorts"));

            var sorsDic = new Dictionary<int, int>();
            sorts.ForEach(kv => sorsDic.Add(kv.Key, kv.Value));
            var result = Menu.UpdateSort(sorsDic);

            return result ? new JsonResult { Data = new ExecuteResult { Result = true } } : new JsonResult { Data = new ExecuteResult { Result = false, Message = result.Message } };
        }

        /// <summary>
        /// Buttons the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="page">The page.</param>
        /// <returns>ActionResult.</returns>
        [Button(MenuCode = "MMenuManage", ButtonCode = "Buttons")]
        public ActionResult Button(int id, int page)
        {
            var iconFolder = AppDomain.CurrentDomain.BaseDirectory + "Areas\\Master\\Common\\icons\\16";
            var files = Directory.GetFiles(iconFolder);
            var dic = new Dictionary<string, string>();
            foreach (var file in files)
            {
                var fileNameWithoutEx = Path.GetFileNameWithoutExtension(file);
                var fileName = Path.GetFileName(file);
                if (fileNameWithoutEx != null)
                    dic.Add(fileNameWithoutEx.Replace('_', '-'), fileName);
            }
            ViewBag.Icons = dic;
            var menu = Menu.Single(id);
            var list = BLL.Master.Button.All(id, page, PageSize);
            ViewBag.RecordCount = list.AllCount;
            ViewBag.MenuID = id;
            ViewBag.Menu = menu;
            return View(list);
        }

        /// <summary>
        /// Buttons the add.
        /// </summary>
        /// <param name="ac">The ac.</param>
        /// <returns>ActionResult.</returns>
        [HttpPost]
        [Button(MenuCode = "MMenuManage", ButtonCode = "Add", IsAjax = true)]
        public ActionResult ButtonAdd(ButtonModel ac)
        {
            var result = BLL.Master.Button.Insert(ac);

            return result ? new JsonResult { Data = new ExecuteResult { Result = true } } : new JsonResult { Data = new ExecuteResult { Result = false, Message = result.Message } };
        }

        /// <summary>
        /// Buttons the single.
        /// </summary>
        /// <param name="buttonId">The button identifier.</param>
        /// <param name="menuId">The menu identifier.</param>
        /// <returns>JsonResult.</returns>
        [HttpPost]
        [Button(MenuCode = "MMenuManage", ButtonCode = "Add,Edit", IsAjax = true)]
        public JsonResult ButtonSingle(int buttonId, int menuId)
        {
            var button = BLL.Master.Button.Single(buttonId);
            return new JsonResult { Data = button };
        }

        /// <summary>
        /// Buttons the edit.
        /// </summary>
        /// <param name="ac">The ac.</param>
        /// <returns>JsonResult.</returns>
        [HttpPost]
        [Button(MenuCode = "MMenuManage", ButtonCode = "Edit", IsAjax = true)]
        public JsonResult ButtonEdit(ButtonModel ac)
        {
            var result = BLL.Master.Button.Update(ac);

            return result ? new JsonResult { Data = new ExecuteResult { Result = true } } : new JsonResult { Data = new ExecuteResult { Result = false, Message = result.Message } };
        }
    }
}
